; ========================================================================
; XY SQUARING MODE TOGGLE
; ========================================================================
; Purpose: Toggle between auto and manual XY squaring modes
; Parameter R: If present, force enable auto mode (sets initial state to manual, then toggles to auto)
; ========================================================================

M98 P"0:/user/xy_square_manual.g"
M98 P"0:/user/xy_square_auto.g"
M98 P"0:/user/xy_square_mode.g"

if !exists(global.xy_square_manual)
  global xy_square_manual = 0.0

if !exists(global.xy_square_auto)
  global xy_square_auto = 0.0

if !exists(global.xy_square_mode)
  global xy_square_mode = "manual"

; ===== If parameter R exists, force initial state to manual (will toggle to auto) =====
if exists(param.R)
  set global.xy_square_mode = "manual"

; ===== Toggle the mode =====
if exists(global.xy_square_mode)
  if global.xy_square_mode == "auto"
    set global.xy_square_mode = "manual"
  else
    set global.xy_square_mode = "auto"
else
  global xy_square_mode = "manual"

var active = 0.0
if global.xy_square_mode == "manual"
  set var.active = global.xy_square_manual
else
  set var.active = global.xy_square_auto

echo >"0:/user/xy_square_mode.g" "if exists(global.xy_square_mode)"
echo >>"0:/user/xy_square_mode.g" "  set global.xy_square_mode = """^{global.xy_square_mode}^""""
echo >>"0:/user/xy_square_mode.g" "else"
echo >>"0:/user/xy_square_mode.g" "  global xy_square_mode = """^{global.xy_square_mode}^""""

echo "XY squaring mode set to "^{global.xy_square_mode}^", active offset "^{var.active}^"mm."

; Prompt user to run Auto Calibration when switching to auto mode (skip if parameter R)
if global.xy_square_mode == "auto" && !exists(param.R)
  M291 S4 K{"Run Auto Calibration Now", "Skip for Now"} F0 R"Mode Changed to Auto" P"XY Squaring mode has been changed to Auto.<br>To get an accurate value, Auto Calibration is required."
  if input == 0
    M98 P"0:/macros/Auto Calibration"